//
//  ZFNormalViewController.m
//  ZFPlayer
//
//  Created by 紫枫 on 2018/3/21.
//  Copyright © 2018年 紫枫. All rights reserved.
//
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "CampusVideoPlayerViewController.h"
#import "ReportViewController.h"
#import "ExercisesAnswerResultViewController.h"
#import "ExercisesPageViewController.h"
#import "CampusVideoControl.h"
#import "XHVideoRateView.h"
#import "XHVideoMoreView.h"

#import "XHVideoFeedbackView.h"
#import "VideoPlayFinishView.h"
#import "VideoNextPlayView.h"
#import "VideoloadingModel.h"
#import "LessonUseageModel.h"
#import "WHToast.h"

#import "VideoShareView.h"

@interface CampusVideoPlayerViewController ()
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) CampusVideoControl *controlView;
@property (nonatomic, strong) UIButton *playBtn;
@property (nonatomic, strong) UIButton *changeBtn;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) NSArray <NSURL *>*assetURLs;


@property (nonatomic,assign)CGFloat currentBrightness;

/// 播放状态
@property (nonatomic,strong)NSNumber *playStatus;

@property (nonatomic,assign)BOOL isFinish;

@end

@implementation CampusVideoPlayerViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.isHideNavBar = YES;
    
    [self setupPlayer];
    

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.player.viewControllerDisappear = NO;

    [self.player enterFullScreen:YES animated:NO];
    
    if (self.playStatus.intValue != 2) {
        [self.player.currentPlayerManager play];
    }else{
        [self.player.currentPlayerManager pause];
    }
  
    

}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
        
    self.player.viewControllerDisappear = YES;
        

    ((AppDelegate *)([UIApplication sharedApplication].delegate)).allowOrentitaionRotation = NO;

    
    //还原竖屏
    //    ((AppDelegate *)[UIApplication sharedApplication].delegate).interfaceOrientation = UIInterfaceOrientationMaskPortrait;
       //强制旋屏，还原竖屏
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    
    [UIViewController attemptRotationToDeviceOrientation];

}

#pragma mark - 外部停止播放
- (void)stopVideoPlayer{
    [self updatePlayerStatus];
    [self.controlView.player.currentPlayerManager pause];
    [self.player enterFullScreen:NO animated:NO];
}
#pragma mark - 销毁新手引导
-(void)removeAllHelpView{
}


#pragma mark - 设置播放器
- (void)setupPlayer {
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.view];
    self.player.controlView = self.controlView;
    self.player.allowOrentitaionRotation = NO;
    self.player.assetURLs = @[[NSURL URLWithString:self.videoUrl]];
    self.player.WWANAutoPlay = YES;
    
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        ((AppDelegate *)([UIApplication sharedApplication].delegate)).allowOrentitaionRotation = isFullScreen;

    };
    // 返回按钮事件
    __weak typeof(self)weakSelf = self;
    /// 播放完成
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
    
        [weakSelf.player.currentPlayerManager replay];

    };
    
    
    
    [self.player playTheIndex:0];
    
    self.controlView.landScapeControlView.backBtnClickCallback = ^{
        
        __strong typeof(weakSelf)strongSelf = weakSelf;
        
        if (strongSelf.player.orientationObserver.supportInterfaceOrientation & ZFInterfaceOrientationMaskPortrait){
           
            [strongSelf.player.currentPlayerManager pause];
            
            [strongSelf.player enterFullScreen:NO animated:NO completion:nil];
            
            [strongSelf dismissViewControllerAnimated:YES completion:nil];
        }
        
    };
    
    
   
    [self.controlView showTitle:@"122" coverURLString:nil fullScreenMode:ZFFullScreenModeAutomatic];
    

}




- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (CampusVideoControl *)controlView {
    if (!_controlView) {
        _controlView = [CampusVideoControl new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.25;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
        _controlView.fullScreenMode = ZFFullScreenModeLandscape;
    }
    return _controlView;
}


- (void)updatePlayerStatus{
    if (self.player.currentPlayerManager.isPlaying) {
        self.playStatus = @(1);
    }else{
        self.playStatus = @(2);
    }
}

@end
